function wrapNodeLabel(nodes) {
    // ensure each line char count <= lineCharCount
    // should be set >=10 or text may appear left-aligned
    const lineCharCount = 12;

    const isAscii = x => x >= 32 && x <= 126;

    for (let i of nodes) {
        let wrappedLabel = "";

        // ascii chars occupy 1 whereas chinese chars occupy 2
        let currentLineCharCount = 0;

        let nextLineStartIndex = 0;

        for (let j = 0; j < i.label.length; j++) {
            const currentLineCharCountIncrement = isAscii(i.label.charCodeAt(j)) ? 1 : 2;

            if (currentLineCharCount + currentLineCharCountIncrement > lineCharCount) {
                wrappedLabel += i.label.slice(nextLineStartIndex, j) + "\n";
                currentLineCharCount = 0;
                nextLineStartIndex = j;
                // if we do not decrease j,
                // then currentLineCharCount of next iteration will not be 0.
                // It will be currentLineCharCountIncrement here.
                j--;
            }
            else {
                currentLineCharCount += currentLineCharCountIncrement;
            }
        }

        wrappedLabel += i.label.slice(nextLineStartIndex);

        i.labelUnwrapped = i.label;
        i.label = wrappedLabel;
    }
}

export { wrapNodeLabel };